package com.hbsd.paopaobackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.mapper.TeamMapper;
import com.hbsd.paopaobackend.model.domain.Team;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.domain.UserTeam;
import com.hbsd.paopaobackend.model.dto.TeamQuery;
import com.hbsd.paopaobackend.model.enums.TeamStatusEnum;
import com.hbsd.paopaobackend.model.request.Team.TeamJoinRequest;
import com.hbsd.paopaobackend.model.request.Team.TeamQuitRequest;
import com.hbsd.paopaobackend.model.request.Team.TeamUploadRequest;
import com.hbsd.paopaobackend.model.vo.TeamUserVO;
import com.hbsd.paopaobackend.model.vo.UserVO;
import com.hbsd.paopaobackend.service.TeamService;
import com.hbsd.paopaobackend.service.UserService;
import com.hbsd.paopaobackend.service.UserTeamService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author zzh
 * @description 针对表【team(队伍表)】的数据库操作Service实现

 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team>
        implements TeamService {

    @Resource
    private UserTeamService userTeamService;

    @Resource
    private UserService userService;

    @Resource
    private RedissonClient redissonClient;


    /*
     * 盐值
     */

    private static final String SALT = "zzh";


    /**
     * 新增队伍
     *
     * @param team
     * @param loginUser
     * @return
     */
    @Override
    // 开启事务 有一个表没有操作成功，对应者表也不会做修改
    @Transactional(rollbackFor = Exception.class)
    public long addTeam(Team team, User loginUser) {
        // 1. 校验请求参数是否为空
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍信息不能为空");
        }

        // 2. 校验登录用户是否为空
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "请先登录");
        }


        // 3. 校验队伍信息

        // 3.1 队伍名称不能为空
        String name = team.getName();
        // 如果名字为空 或者 名字长度超过20
        if (StringUtils.isBlank(name) || name.length() > 20) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍名称不符合要求");
        }

        // 3.2 队伍最大人数<=20
        int maxNum = Optional.ofNullable(team.getMaxNum()).orElse(0);
        if (maxNum < 1 || maxNum > 20) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍人数不符合要求");
        }

        String description = team.getDescription();
        if (StringUtils.isNotBlank(description) && description.length() > 512) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍描述过长");
        }


        // 4. status是否公开(int) 不传默认为0 (公开)
        int teamStatus = Optional.ofNullable(team.getTeamStatus()).orElse(0);
        TeamStatusEnum statusEnum = TeamStatusEnum.getEnumByValue(teamStatus);
        if (statusEnum == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍状态不符合要求");
        }

        // 5.如果 status 是加密状态，一定要有密码，且密码<=32
        String password = team.getPassword();
        if (TeamStatusEnum.SECRET.equals(statusEnum)) {
            // 密码为空 或者 大于32位 进入if语句
            if (StringUtils.isBlank(password) || password.length() > 32) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码设置不正确");
            }
        }

        String teamPic = team.getTeamPic();
        if (StringUtils.isBlank(teamPic)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请上传队伍图片");
        }


        // 6、超时时间 > 当前时间
        Date expireTime = team.getExpireTime();
        if (expireTime == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请填写超时时间");
        }
        //  当前时间 在 超时时间后面
        if (new Date().after(expireTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "超时时间设置不正确");
        }


        // 7、校验用户最多创建 5个队伍
        // todo 有 bug，可鞥同时创建100个队伍
        Long userId = loginUser.getId();
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        long hasTeamNum = this.count(queryWrapper);
        if (hasTeamNum >= 5) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户最多创建5个队伍");
        }

        // 8、插入队伍信息到队伍表
        team.setId(null);
        team.setUserId(userId);
        String encryptPassword = encryptPassword(password);
        team.setPassword(encryptPassword);
        team.setJoinNum(1);
        boolean result = this.save(team);
        Long teamId = team.getId();


        if (!result || teamId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍信息保存失败");
        }

        // 9、插入用户和队伍的关联关系
        UserTeam userTeam = new UserTeam();
        userTeam.setUserId(userId);
        userTeam.setTeamId(teamId);
        userTeam.setJoinTime(new Date());


        result = userTeamService.save(userTeam);

        if (!result) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户和队伍的关联关系保存失败");
        }

        return teamId;

    }

    /**
     * 搜索查询（条件查询）
     *
     * @param teamQuery
     * @param isAdmin
     * @return
     */
    @Override
    public List<TeamUserVO> listTeams(TeamQuery teamQuery, Boolean isAdmin) {
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        // 1、组合查询条件
        if (teamQuery != null) {
            Long id = teamQuery.getId();
            if (id != null && id > 0) {
                queryWrapper.eq("id", id);
            }

            List<Long> idList = teamQuery.getIdList();
            if(CollectionUtils.isNotEmpty(idList)) {
                queryWrapper.in("id", idList);
            }

            String searchText = teamQuery.getSearchText();
            if (StringUtils.isNotBlank(searchText)) {
                queryWrapper.and(qw -> qw.like("name", searchText).or().like("description", searchText));

            }

            String name = teamQuery.getName();
            if (StringUtils.isNotBlank(name)) {
                queryWrapper.like("name", name);
            }

            String description = teamQuery.getDescription();
            if (StringUtils.isNotBlank(description)) {
                queryWrapper.like("description", description);
            }

            Integer maxNum = teamQuery.getMaxNum();
            if (maxNum != null && maxNum > 0) {
                queryWrapper.eq("maxNum", maxNum);
            }

            Long userId = teamQuery.getUserId();
            if (userId != null && userId > 0) {
                queryWrapper.eq("userId", userId);
            }

            // 前端传来的状态
            Integer teamStatus = teamQuery.getTeamStatus();
            // 根据状态改为枚举(0、1、2)
            TeamStatusEnum statusEnum = TeamStatusEnum.getEnumByValue(teamStatus);
            // 如果状态为空，默认为公开
            if (teamStatus == null) {
                statusEnum = TeamStatusEnum.PUBLIC;
            }
            // 查询的用户，既不是管理员，他查询的数据也不是公开的，需要抛出异常
            if (!isAdmin && !statusEnum.equals(TeamStatusEnum.PUBLIC)) {
                throw new BusinessException(ErrorCode.NO_AUTH, "非管理员只能查询公开的队伍");
            }


            queryWrapper.eq("teamStatus", statusEnum.getValue());


        }

        // 不展示过期的队伍
        // expireTime is null or expireTime > now() 过期时间为空，代表查询所有，有值则必须大于当前时间
        // lt(): 小于
        // gt(): 大于
        queryWrapper.and(qw -> qw.gt("expireTime", new Date()).or().isNull("expireTime"));
        List<Team> teamList = this.list(queryWrapper);

        if (CollectionUtils.isEmpty(teamList)) {
            return new ArrayList<>();
        }

        System.out.println("teamList = " + teamList);
        List<TeamUserVO> teamUserVOList = new ArrayList<>();
        // 关联表查询常见人用户信息
        for (Team team : teamList) {
            Long userId = team.getUserId();
            System.out.println("userId = " + userId);
            if (userId == null) {
                continue;
            }
            User user = userService.getById(userId);
            if(user == null) {
                throw new BusinessException(ErrorCode.NULL_ERROR, "用户不存在");
            }
            TeamUserVO teamUserVO = new TeamUserVO();
            try {
                BeanUtils.copyProperties(teamUserVO, team);
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "查询失败1");
            }
            UserVO userVO = new UserVO();
            try {
                BeanUtil.copyProperties(user, userVO, "tags");
                userVO.setTags(Objects.nonNull(user.getTags()) ? JSONUtil.parseArray(user.getTags()).toList(String.class) : new ArrayList<>());
//                BeanUtils.copyProperties(userVO, user);
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "查询失败2");
            }
            teamUserVO.setCreateUser(userVO);

            teamUserVOList.add(teamUserVO);

        }

        return teamUserVOList;

        //select *
        //from team t
        //         left join user_team ut on t.id = ut.teamId
        //         left join user u on ut.userId = u.id;

    }

    /**
     * 更新队伍信息
     *
     * @param teamUploadRequest
     * @param loginUser
     * @param isAdmin
     * @return
     */
    // 开启事务 有一个表没有操作成功，对应者表也不会做修改
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTeam(TeamUploadRequest teamUploadRequest, User loginUser, boolean isAdmin) {
        // 1、校验请求参数是否为空
        if (teamUploadRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍信息不能为空");
        }


        // 2、判断队伍是否存在
        Long id = teamUploadRequest.getId();
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍id不能为空");
        }

        Team oldTeam = this.getById(id);
        if (oldTeam == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "队伍不存在");
        }



        // 只有管理员 和 创建者 可以修改队伍信息
        if (!isAdmin && !oldTeam.getUserId().equals(loginUser.getId())) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非管理员或创建者不能修改队伍信息");
        }

        TeamStatusEnum statusEnum = TeamStatusEnum.getEnumByValue(teamUploadRequest.getTeamStatus());
        if (statusEnum.equals(TeamStatusEnum.SECRET)) {
            // 密码不能为空
            if (StringUtils.isBlank(teamUploadRequest.getPassword())) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "加密队伍必须设置密码");
            }
        }

        Team team = new Team();
        try {
            BeanUtils.copyProperties(team, teamUploadRequest);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数复制失败");
        }


        String password = teamUploadRequest.getPassword();
        if (password != null) {

            String encryptPassword = encryptPassword(password);
            team.setPassword(encryptPassword);
        }



        boolean result = this.updateById(team);
        return result;

    }

    /**
     * 加入队伍
     *
     * @param teamJoinRequest
     * @param loginUser
     * @return
     */
    // 开启事务 有一个表没有操作成功，对应者表也不会做修改
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean joinTeam(TeamJoinRequest teamJoinRequest, User loginUser) {
        // 获取到用户id
        Long userId = loginUser.getId();
        QueryWrapper<UserTeam> queryWrapper = new QueryWrapper<>();
        // 1、计算该用户已经加入了几个队伍 超过5个，则抛出异常
        queryWrapper.eq("userId", userId);
        //
        long count = userTeamService.count(queryWrapper);
        if (count > 5) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户最多创建和加入 5 个队伍");
        }

        // 2、队伍必须存在，只能加入未满，未过期的队伍
        //找到要加入的队伍id
        Long teamId = teamJoinRequest.getTeamId();
        if (teamId == null || teamId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "该队伍不存在");
        }

        // 求出该队伍的最大人数
        Team team = this.getById(teamId);
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "队伍不存在");
        }

        // 队伍可存在的最多人数
        Integer maxNum = team.getMaxNum();

        // 找到该队伍已经存在几个人
        QueryWrapper<UserTeam> teamQueryWrapper = new QueryWrapper<>();
        teamQueryWrapper.eq("teamId", teamId);
        long teamCount = userTeamService.count(teamQueryWrapper);

        // 队伍已满
        if (teamCount >= maxNum) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍已满");
        }

        // 找到该队伍的过期时间
        Date expireTime = team.getExpireTime();
        if (expireTime != null && new Date().after(expireTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍已过期");
        }


        // 分布式锁
        RLock lock = redissonClient.getLock("paopao:join_team");
        try {
            // 用来防止死锁
            int i = 0;
            // 第二个参数: 就是锁的过期时间 这里设置-1的原因，是因为你不能手动设置锁的过期时间，因为看门狗会自动续期，所以这里设置-1，让redisson自动续期。30s后自动释放锁。 解决第三个问题
            boolean tryLock = lock.tryLock(0, -1, TimeUnit.MILLISECONDS);
            while(true) {
                // 只有一个线程能获取锁
                if(tryLock) {
                    i++;
                    if(i > 5) {
                        throw new BusinessException(ErrorCode.PARAMS_ERROR, "获取锁失败");
                    }
                    System.out.println("getLock = " + Thread.currentThread().getId());
                    // 3、不能重复加入已加入的队伍
                    QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
                    // 根据 用户id和队伍id进行搜索，查询该用户是否加入了该队伍
                    userTeamQueryWrapper.and(qw -> qw.eq("userId", userId).eq("teamId", teamId));
                    long joinByUser = userTeamService.count(userTeamQueryWrapper);

                    if (joinByUser >= 1) {
                        throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能重复加入队伍");
                    }

                    // 4、禁止加入私有的队伍
                    Integer teamStatus = team.getTeamStatus();
                    TeamStatusEnum teamStatusEnum = TeamStatusEnum.getEnumByValue(teamStatus);
                    if (TeamStatusEnum.PRIVATE.equals(teamStatusEnum)) {
                        throw new BusinessException(ErrorCode.PARAMS_ERROR, "禁止加入私有队伍");
                    }

                    // 5、如果队伍是加密的，必须密码匹配才可以
                    // 进入队伍的密码
                    String teamPassword = team.getPassword();
                    // 用户加入队伍，传递的密码
                    String joinRequestPassword = teamJoinRequest.getPassword();
                    // 加密

                    String md5DigestAsHex = encryptPassword(joinRequestPassword);
                    if (TeamStatusEnum.SECRET.equals(teamStatusEnum)) {
                        if (StringUtils.isNotBlank(md5DigestAsHex) && !teamPassword.equals(md5DigestAsHex)) {
                            throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码不正确");
                        }
                    }

                    // 6、新增队伍信息，用户关联信息表
                    UserTeam userTeam = new UserTeam();
                    userTeam.setUserId(userId);
                    userTeam.setTeamId(teamId);
                    userTeam.setJoinTime(new Date());
                    boolean save = userTeamService.save(userTeam);
                    if (!save) {
                        throw new BusinessException(ErrorCode.PARAMS_ERROR, "加入队伍失败");
                    }

                    QueryWrapper<Team> teamQueryWrapper1 = new QueryWrapper<>();
                    teamQueryWrapper1.eq("id", teamId);
                    Team team1 = this.getOne(teamQueryWrapper1);
                    team1.setJoinNum(team1.getJoinNum() + 1);

                    boolean result = this.updateById(team1);
                    if (!result) {
                        throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新队伍人数失败");
                    }
                    return result;


                    // 这里打断点就是为了，自动续期锁
//                Thread.sleep(1000000);
//                System.out.println("getLock = " + Thread.currentThread().getId());
//                for (Long userId : mainUserList) {
//                    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//                    // 分页查询
//                    Page<User> userPage = userService.page(new Page<>(1, 10), queryWrapper);
//
//                    // 缓存数据
//                    String redisKey = String.format("paopao:user:recommend:%s",  userId);
//                    // 获取操作对象
//                    ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();        // 任务逻辑
//                    // 缓存数据
//                    try {
//                        // 设置了过期时间: 10000ms = 10s
//                        // 1天后过期
//                        valueOperations.set(redisKey, userPage,1, TimeUnit.DAYS); // 解决第二个问题
//                    } catch (Exception e) {
//                        log.info("redis set key error", e);
//                    }
//                }
                }


            }

        } catch (InterruptedException e) {
            log.error("doCacheRecommendUser error: ", e);
            return false;
        } finally {
            // 只能释放自己的锁  解决第四个问题
            if(lock.isHeldByCurrentThread()) {
                System.out.println("unLock = " + Thread.currentThread().getId());
                lock.unlock();  // 解决第一个问题
            }
        }





    }

    /**
     * 退出队伍
     *
     * @param teamQuitRequest
     * @param loginUser
     * @return
     */
    // 开启事务 有一个表没有操作成功，对应者表也不会做修改
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean quitTeam(TeamQuitRequest teamQuitRequest, User loginUser) {
        // 1、校验请求参数是否为空
        if (teamQuitRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        }

        // 2、校验队伍是否存在
        Long teamId = teamQuitRequest.getTeamId();
        Team team = this.getById(teamId);
        if (teamId == null || teamId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍不存在");
        }
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "队伍不存在");
        }
        // 3、检测我是否已经加入该队伍
        Long userId = loginUser.getId();
        QueryWrapper<UserTeam> queryWrapper = new QueryWrapper<>();
        // 只有用户名和队伍名称相同，才能说明你在该队伍里面
        queryWrapper.eq("userId", userId).eq("teamId", teamId);
        List<UserTeam> list = userTeamService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "你没有加入该队伍");
        }


        // 4、队伍
        // 4-1、队伍只剩一人，解散队伍
        QueryWrapper<UserTeam> teamCountQueryWrapper = new QueryWrapper<>();
        teamCountQueryWrapper.eq("teamId", teamId);
        long count = userTeamService.count(teamCountQueryWrapper);

        if (count == 1) {
            // 4-1-1、清掉队伍表里的队伍数据
            boolean teamResult = this.removeById(teamId);
            // 4-1-1、清掉用户队伍表里的数据
            boolean userTeamResult = userTeamService.remove(queryWrapper);
            if (!teamResult || !userTeamResult) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "解散队伍失败");
            } else {
                return true;
            }

        }

        // 4-2、还有其他人
        // 4-2-1、如果是队长退出，则权限转交给第二个人(先来后到)
        if (team.getUserId().equals(userId)) {
            // 1、查询已加入队伍的所有用户和加入时间
            QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
            userTeamQueryWrapper.eq("teamId", teamId);
            // 2、按加入时间排序
            userTeamQueryWrapper.last("order by id asc limit 2");
            List<UserTeam> userTeamList = userTeamService.list(userTeamQueryWrapper);

            if (CollectionUtils.isEmpty(userTeamList) && userTeamList.size() < 2) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            UserTeam nextUserTeam = userTeamList.get(1);


            Long nextUserTeamUserId = nextUserTeam.getUserId();
            // 更新队长
            Team updateTeam = new Team();
            updateTeam.setId(teamId);
            updateTeam.setUserId(nextUserTeamUserId);
            boolean result = this.updateById(updateTeam);
            if (!result) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新队长失败");
            }

            // 移除关联表数据
            QueryWrapper<UserTeam> removeQueryWrapper = new QueryWrapper<>();
            removeQueryWrapper.eq("teamId", teamId).eq("userId", userId);
            boolean remove = userTeamService.remove(removeQueryWrapper);
            if (!remove) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "退出队伍失败");
            } else {
                return true;
            }
        }
        // 4-2-2、非队长自己退出
        boolean result = userTeamService.remove(queryWrapper);
        // 将joinNum - 1, 因为有人退出了
        Integer joinNum = team.getJoinNum();
        team.setJoinNum(joinNum-1);
        boolean updateById = this.updateById(team);
        if (!updateById) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新队伍人数失败");
        }

        if (!result) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "退出队伍失败");
        }



        return true;

    }

    /**
     * 解散队伍
     *
     * @param teamId
     * @param loginUser
     * @return
     */
    // 开启事务 有一个表没有操作成功，对应者表也不会做修改
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean dissolveTeam(Long teamId, User loginUser) {
        // 1、校验请求参数是否为空
        if (teamId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "队伍不存在");
        }

        // 2、校验队伍是否存在
        QueryWrapper<Team> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", teamId);
        long count = this.count(queryWrapper);
        // 查看队伍是否存在
        if (count != 1) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "队伍不存在");
        }

        // 3、校验你是不是队长
        Team team = this.getById(teamId);
        Long userId = team.getUserId();
        if (!loginUser.getId().equals(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "你不是队长，不能解散队伍");
        }

        // 4、移除所有加入队伍的关联信息
        QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<>();
        userTeamQueryWrapper.eq("teamId", teamId);
        boolean result = userTeamService.remove(userTeamQueryWrapper);
        if (!result) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "解散队伍失败");
        }

        // 5、删除队伍
        boolean dissolveResult = this.removeById(teamId);
        if (!dissolveResult) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "解散队伍失败");
        }
        return true;

    }

    @Override
    public List<UserTeam> getCurrentJoinTeamList(TeamQuery teamQuery, User loginUser) {
        // 1、校验请求参数是否为空
        if(teamQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不能为空");
        }

        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH, "用户未登录");
        }

        QueryWrapper<UserTeam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", loginUser.getId());
        List<UserTeam> list = userTeamService.list(queryWrapper);
        return list;
    }

    /**
     * 加密密码
     *
     * @param password
     * @return
     */
    private String encryptPassword(String password) {
        return DigestUtils.md5DigestAsHex((SALT + password).getBytes());
    }

}




